const router = require('koa-router')();
const logHelper=require('../utils/logHelper').helper;
const UserService=require('../service/UserService');

router.prefix('/User');


/**
 * @api {post} /User/Login  教师账号密码登录
 * @apiGroup User
 * @apiName Login
 * @apiParam {String} TeacherUserAccount    教师账号
 * @apiParam {String} TeacherUserPassword    教师密码
 * @apiSuccess {String} code      (0参数不齐||1查询成功||2查询无记录||3数据库报错||4服务器报错)
 * @apiSuccess {Array} DATA
 * @apiSuccess {String} DATA.UserAccount          用户账号
 */
router.post('/Login', async (ctx, next) =>{
    let data=ctx.request.body
    if(!data.TeacherUserAccount||!data.TeacherUserPassword){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/Login',data);
    try {
        let res=await UserService.Login(data);
        logHelper.endLog('/User/Login',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/Login',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


/**
 * @api {post} /User/TeacherSendVerification  教师手机号发送验证码(登录发送)
 * @apiGroup User
 * @apiName TeacherSendVerification
 * @apiParam {String} Token    Token值
 * @apiParam {String} TeacherUserMobile    教师手机号
 * @apiSuccess {String} code      (0参数不齐||1发送成功||2验证码存入失败||3该账号不存在||4服务器报错)
 */
router.post('/TeacherSendVerification', async (ctx, next) =>{
    let data=ctx.request.body
    if(!data.TeacherUserMobile){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/TeacherSendVerification',data);
    try {
        let res=await UserService.TeacherSendVerification(data);
        logHelper.endLog('/User/TeacherSendVerification',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/TeacherSendVerification',e.message);
        ctx.body={code:4,msg:e.message}
    }
})



/**
 * @api {post} /User/UpdateTeacherUserName  教师修改昵称
 * @apiGroup User
 * @apiName UpdateTeacherUserName
 * @apiParam {String} Token    Token值
 * @apiParam {String} TeacherUserName    教师昵称
 * @apiSuccess {String} code      (0参数不齐||1修改成功||2修改失败||3数据库报错||4服务器报错)
 */
router.post('/UpdateTeacherUserName', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.TeacherUserId||!data.TeacherUserName){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/UpdateTeacherUserName',data);
    try {
        let res=await UserService.UpdateTeacherUserName(data);
        logHelper.endLog('/User/UpdateTeacherUserName',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/UpdateTeacherUserName',e.message);
        ctx.body={code:4,msg:e.message}
    }
})



/**
 * @api {post} /User/UpdateTeacherUserMobile  教师修改手机号
 * @apiGroup User
 * @apiName UpdateTeacherUserName
 * @apiParam {String} Token    Token值
 * @apiParam {String} TeacherUserMobile    教师新手机号
 * @apiSuccess {String} code      (0参数不齐||1修改成功||2修改失败||3数据库报错||4服务器报错)
 */
router.post('/UpdateTeacherUserMobile', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.TeacherUserId||!data.TeacherUserMobile){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/UpdateTeacherUserMobile',data);
    try {
        let res=await UserService.UpdateTeacherUserMobile(data);
        logHelper.endLog('/User/UpdateTeacherUserMobile',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/UpdateTeacherUserMobile',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


/**
 * @api {post} /User/UpdatePassword  教师修改密码并做密码验证
 * @apiGroup User
 * @apiName UpdatePassword
 * @apiParam {String} Token    Token值
 * @apiParam {String} OldTeacherUserPassword    旧密码
 * @apiParam {String} NewTeacherUserPassword    新密码
 * @apiSuccess {String} code      (0参数不齐||1修改成功||2密码错误||3修改失败||4服务器报错)
 */
router.post('/UpdatePassword', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.TeacherUserId||!data.OldTeacherUserPassword||!data.NewTeacherUserPassword){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/UpdatePassword',data);
    try {
        let res=await UserService.UpdatePassword(data);
        logHelper.endLog('/User/UpdatePassword',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/UpdatePassword',e.message);
        ctx.body={code:4,msg:e.message}
    }
})


/**
 * @api {post} /User/UpdateTeacherUserAvatar  修改教师头像
 * @apiGroup User
 * @apiName UpdateTeacherUserAvatar
 * @apiParam {String} Token    Token值
 * @apiParam {String} Date    图片上层目录的年月日(YYYYMMDD)
 * @apiParam {String} FileName    文件名
 * @apiSuccess {String} code      (0参数不齐||1修改成功||2文件移动失败||3修改失败||4服务器报错)
 */
router.post('/UpdateTeacherUserAvatar', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.Date||!data.FileName){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/UpdateTeacherUserAvatar',data);
    try {
        let res=await UserService.UpdateTeacherUserAvatar(data);
        logHelper.endLog('/User/UpdateTeacherUserAvatar',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/UpdateTeacherUserAvatar',e.message);
        ctx.body={code:4,msg:e.message}
    }
})




/**
 * @api {post} /User/QueryUpgrade  教师升级条件查询
 * @apiGroup User
 * @apiName QueryUpgrade
 * @apiParam {String} Token    Token值
 * @apiSuccess {String} code      (0参数不齐||1查询成功|2查询失败|4服务器报错)
 * @apiSuccess {Array} data        数据
 * @apiSuccess {String} data.TeacherUserGrade        教师等级
 * @apiSuccess {String} data.TeacherUserFans        粉丝数
 * @apiSuccess {String} data.LectureFrequency        讲课次数
 * @apiSuccess {String} data.TeacherListenNumber        收听人数
 */
router.post('/QueryUpgrade', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.TeacherUserId){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/QueryUpgrade',data);
    try {
        let res=await UserService.QueryUpgrade(data);
        logHelper.endLog('/User/QueryUpgrade',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/QueryUpgrade',e.message);
        ctx.body={code:4,msg:e.message}
    }
})




/**
 * @api {post} /User/QueryTeacherUser  根据用户id查询教师信息
 * @apiGroup User
 * @apiName QueryTeacherUser
 * @apiParam {String} Token    Token值
 * @apiSuccess {String} code      (0参数不齐||1查询成功|2查询失败|4服务器报错)
 * @apiSuccess {Array} data        数据
 * @apiSuccess {String} data.TeacherUserName        教师昵称
 * @apiSuccess {String} data.TeacherUserAvatar        教师头像
 * @apiSuccess {String} data.TeacherUserMobile        教师手机号
 * @apiSuccess {String} data.TeacherUserAccount        教师账号
 * @apiSuccess {String} data.TeacherUserGrade        教师等级
 */
router.post('/QueryTeacherUser', async (ctx, next) =>{
    let data=ctx.request.body;
    if(!data.TeacherUserId){
        ctx.body={code:0,msg:'参数不齐'};
        return
    }
    logHelper.startLog('/User/QueryTeacherUser',data);
    try {
        let res=await UserService.QueryTeacherUser(data);
        logHelper.endLog('/User/QueryTeacherUser',res);
        ctx.body=res;
    }catch (e) {
        logHelper.endErrLog('/User/QueryTeacherUser',e.message);
        ctx.body={code:4,msg:e.message}
    }
})



module.exports=router;
